package com.ruike.alisurface.http;

import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.ApiTools;
import com.voodoo.lib_frame.httpTools.RequestParams;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONException;
import org.json.JSONObject;

import static com.ruike.alisurface.Constant.DefaultData.TEST_CAMERA_MCHID;

/**
 * Author：voodoo
 * CreateTime：2021/06/18 018 上午 09:15
 * Description：
 */
public class WxPayHttp {

    /**
     * 微信创建订单，测试摄像头使用
     */
    public static void createOrder(String orderDetails, BaseView apiListener) {
        RequestParams params = new RequestParams();
        params.put("MchId", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        params.put("OrderDetails", orderDetails);
        params.put("is_micropay", true); // 微信刷脸必填，值为true
        L.i(String.format("请求创建订单接口：%s：\n参数：MchId=%s  OrderDetails=%s  is_micropay=%s",
                UrlHttpUtils.CREATE_ORDER_URL,
                ShareUtils.getInstance().getString(ShareKey.MCH_ID),
                orderDetails,
                true));
        new ApiTools().postRequest(UrlHttpUtils.CREATE_ORDER_URL, params, apiListener);
    }

    /**
     * 将获取到的 rawdata 上传给后台
     */
    public static void getAuthinfo(String rawdata, BaseView apiListener, boolean... isTests) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("mchid", (isTests != null && isTests.length > 0 && isTests[0]) ? TEST_CAMERA_MCHID : ShareUtils.getInstance().getString(ShareKey.MCH_ID));
            jsonObject.put("rawdata", rawdata);
            new ApiTools().postJsonRequest(UrlHttpUtils.GET_AUTH_INFO_URL, jsonObject.toString(), apiListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将获取到的 facecode 上传给后台
     */
    public static void createMicropay(String orderId, String facecode, BaseView apiListener) {
        RequestParams params = new RequestParams();
        params.put("order_id", orderId);
        params.put("auth_code", facecode);
        new ApiTools().getRequest(UrlHttpUtils.CREATE_MICROPAY_URL, params, apiListener);
    }

    /**
     * 获取微信的支付结果
     */
    public static void getWechatPayResult(String orderId, String transaction_id, BaseView apiListener) {
        RequestParams params = new RequestParams();
        params.put("order_id", orderId);
        params.put("transaction_id", transaction_id);
        new ApiTools().getRequest(UrlHttpUtils.POLLING_WECHAT_PAY_RESULT, params, apiListener);
    }

}
